package cn.lixu.order.controller;

import cn.lixu.basic.annotation.PreAuthorize;
import cn.lixu.basic.util.JsonResult;
import cn.lixu.basic.util.PageList;
import cn.lixu.order.domain.OrderPetAcquisition;
import cn.lixu.order.dto.OrderDto;
import cn.lixu.order.query.OrderPetAcquisitionQuery;
import cn.lixu.order.service.IOrderPetAcquisitionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //controller层  返回的是json格式数据
@RequestMapping("/orderPetAcquisition")
public class OrderPetAcquisitionController {

    @Autowired
    private IOrderPetAcquisitionService orderPetAcquisitionService;


    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部")
    @PreAuthorize(name = "查询全部订单列表", sn = "orderPetAcquisition:list") //value后面用来做权限校验
    public List<OrderPetAcquisition> loadAll() {
        return orderPetAcquisitionService.loadAll();
    }

    /**
     * 查询一个
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一个")
    @PreAuthorize(name = "查询一个订单列表", sn = "orderPetAcquisition:loadById") //value后面用来做权限校验
    public OrderPetAcquisition loadById(@PathVariable("id") Long id) {
        return orderPetAcquisitionService.loadById(id);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除", sn = "orderPetAcquisition:delete") //value后面用来做权限校验
    public JsonResult deleteById(@PathVariable("id") Long id) {
        try {
            orderPetAcquisitionService.remove(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * @param orderPetAcquisition
     * @return
     */
    @PutMapping
    public JsonResult addAndUpdate(@RequestBody OrderPetAcquisition orderPetAcquisition) {
        try {
            if (orderPetAcquisition.getId() == null) {
                orderPetAcquisitionService.save(orderPetAcquisition);
            } else {
                orderPetAcquisitionService.update(orderPetAcquisition);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页数据
     *
     * @param orderPetAcquisitionQuery
     * @return
     */
    @PostMapping
    public PageList<OrderPetAcquisition> queryPage(@RequestBody OrderPetAcquisitionQuery orderPetAcquisitionQuery) {
        return orderPetAcquisitionService.queryPage(orderPetAcquisitionQuery);

    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @PatchMapping
    @PreAuthorize(name = "批量删除", sn = "orderPetAcquisition:delete") //value后面用来做权限校验
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            orderPetAcquisitionService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }


    /**
     * 订单确认接口
     *
     * @param dto
     * @return
     */
    @PostMapping("/confirm")
    @ApiOperation(value = "确认订单接口")
    public JsonResult confirm(@RequestBody OrderDto dto) {
        System.out.println(dto);
        try {
            orderPetAcquisitionService.confirm(dto);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常，稍候重试");
        }

    }

}

